<?php

use App\Models\Order;
use App\Models\User;
use Illuminate\Database\Seeder;

class OrderTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $user_ids = User::all()->pluck('id')->toArray();

        // 获取 Faker 实例
        $faker = app(Faker\Generator::class);

        // 生成数据集合
        $orders = factory(Order::class)
            ->times(100)
            ->make()
            ->each(function($order)
            use ($faker,$user_ids)
            {
                $order->user_id = $faker->randomElement($user_ids);
            });

        // 让隐藏字段可见，并将数据集合转换为数组
        $order_array = $orders->toArray();

        // 插入到数据库中
        Order::insert($order_array);
    }
}
